﻿var BomEdit = {};
//设置
BomEdit.settings = {
    TaskId: com.get.TaskId,
    TaskCode: "",
    Action: null,
    editRow: undefined,
    ColumnsParam: [],
    currentTr: null,
    EquipId: 0,
    CopySwitch: "1",//0代表复制开关打开，1代表关闭
};

BomEdit.init = function () {
    $("#TaskId").val(BomEdit.settings.TaskId);
    //加载任务信息
    com.ajax({
        url: "/Logic/TM/TaskManagerAjax.aspx?method=GetTaskInfoByPrimaryId&TaskId=" + BomEdit.settings.TaskId, success: function (data) {
            $("#lblTaskCode").html(data.TaskCode);
            $("#lblProjName").html(data.ProjName);
            $("#lblEngName").html(data.EngName);
            $("#lblNum").html(data.TaskNum);
            $("#lblEquipName").html(data.EquipName);
            $("#lblEquipModel").html(data.EquipModel);
            $("#hidEquipId").val(data.EquipId);
            BomEdit.settings.Action = com.get.action;
            BomEdit.settings.TaskCode = data.TaskCode;
            BomEdit.settings.EquipId = data.EquipId;
            BomEdit.settings.EquipModel = data.EquipModel;
        }
    });


    var height = $("#divTotal").height();
    var master = $("#master").height();
    var content = parseFloat(height) - parseFloat(master) - 70;
    $("#divTableBody").css("height", content);


    //加载操作列的显示隐藏数据
    com.ajax({
        url: '/Logic/TM/OrgDataAjax.aspx?method=GetPersonColumn', success: function (data) {
            BomEdit.settings.ColumnsParam = data;
            if (BomEdit.settings.Action == "add") {
                $("#switchCopy").linkbutton('disable');
                $("#XuhaoModel").combobox("setValue", "0");
                BomEdit.LoadOrgOperTable(BomEdit.settings.ColumnsParam, 20);
                BomEdit.SetSequence();
            }
            else if (BomEdit.settings.Action == "batch") {
                $("#switchCopy").linkbutton('disable');
                $("#XuhaoModel").combobox("setValue", "0");
                var Ids = com.get.Ids;
                com.ajax({
                    url: "/Logic/TM/OrgDataAjax.aspx?method=GetOrgCopyDateByIds",
                    data: { Ids: Ids },
                    success: function (rows) {
                        BomEdit.LoadOrgOperTable(BomEdit.settings.ColumnsParam, rows.length);
                        for (var i = 0; i < rows.length; i++) {
                            var tr = $("#tbOperate tr:eq(" + i + ")");
                            BomEdit.LoadData(rows[i], tr);
                            BomEdit.SetSequence();
                        }
                    }
                });
            }
            else if (BomEdit.settings.Action == "edit" || BomEdit.settings.Action == "editMs") {
                $("#XuhaoModel").combobox("setValue", "1");
                var Ids = com.get.orgIds;
                com.ajax({
                    url: "/Logic/TM/OrgDataAjax.aspx?method=GetOrgDateByIds",
                    data: { Ids: Ids },
                    success: function (rows) {
                        BomEdit.LoadOrgOperTable(BomEdit.settings.ColumnsParam, rows.length);
                        for (var i = 0; i < rows.length; i++) {
                            var tr = $("#tbOperate tr:eq(" + i + ")");
                            BomEdit.LoadData(rows[i], tr);
                            tr.find("input[name=Xuhao]").attr("readonly", "readonly");
                            if (com.get.action == "editMs") {
                                tr.find("#MaterialCode").combogrid("disable");
                                tr.find("input[name=Length]").attr("disabled", "disabled");
                                tr.find("input[name=Width]").attr("disabled", "disabled");
                                tr.find("input[name=SingleNum]").attr("disabled", "disabled");
                                tr.find("input[name=Num]").attr("disabled", "disabled");
                                tr.find("input[name=MaUnitWeight]").attr("disabled", "disabled");
                                tr.find("input[name=MaTotalWeight]").attr("disabled", "disabled");
                                tr.find("input[name=TotalLength]").attr("disabled", "disabled");
                                tr.find("#MaState").combobox("disable");
                                tr.find("#IsFixedSize").combobox("disable");
                                tr.find("#IsSubmitMp").combobox("disable");
                            }

                        }
                        BomEdit.SetSequence();
                        $("#Insert").linkbutton('disable');
                        $("#Delete").linkbutton('disable');
                    }
                });
            }
        }
    });


    //详细备注弹窗
    $("#divMainNote").dialog({
        title: '详细备注',
        width: 450,
        height: 200,
        collapsible: true,
        minimizable: false,
        closable: true,
        cache: false,
        modal: true,
        buttons: [{
            text: "确定", icon: "icon-save", handler: function () {
                var mainNote = $("#mainNote").val();
                BomEdit.settings.currentTr.find("#Note").val(mainNote);
                $("#divMainNote").dialog("close");
            }
        }, { text: "取消", handler: function () { $("#divMainNote").dialog("close"); } }]
    })
    $("#divMainNote").dialog("close");

    //初始化增加前缀序号界面
    $('#divBatchEdit').dialog({
        title: '批量替换',
        width: 450,
        height: 120,
        collapsible: true,
        minimizable: false,
        closable: true,
        cache: false,
        modal: true,
        buttons: [{
            text: "确定", icon: "icon-save", handler: function () {
                var field = $("#field").combobox("getValue");
                $("#divTableBody input[name=" + field + "]").each(function () {
                    var value = $(this).val();
                    var before = $("#before").val();
                    var after = $("#after").val();
                    var s = value.replace(before, after);
                    $(this).val(s);
                });
                var XuhaoModel = $("#XuhaoModel").combobox("getValue");

                if (field == "Xuhao" && XuhaoModel == "0") {
                    $("#divTableBody input[name=Zongxu]").each(function () {
                        var value = $(this).val();
                        var before = $("#before").val();
                        var after = $("#after").val();
                        var s = value.replace(before, after);
                        $(this).val(s);
                    });
                }
            }
        }, { text: "取消", handler: function () { $("#divBatchEdit").dialog("close"); } }]
    });
    $("#divBatchEdit").dialog("close");
}


//切换复制开关
BomEdit.EditCopySwitch = function () {
    if (BomEdit.settings.CopySwitch == "0") {
        BomEdit.settings.CopySwitch = "1";
        $("#CopySwitch").html("关");
    }
    else {
        BomEdit.settings.CopySwitch = "0";
        $("#CopySwitch").html("开");
    }

}



BomEdit.LoadOrgOperTable = function (columnData, num) {
    $("#tbOperateHead").html("");
    $("#tbOperate").html("");
    var $tr = $("<tr></tr>");
    for (var i = 0; i < columnData.length; i++) {
        var display = "";
        if (columnData[i].IsHidden == "1") {
            display = ";display:none";
        }
        var th = " <th style='text-align:center" + display + "'><div style='width:" + columnData[i].Length + "px'>" +
           columnData[i].Title + "</div></th>"
        $tr.append($(th));
    }
    $("#tbOperateHead").append($tr);

    BomEdit.AddOperationRow(num, columnData);
}
//删除行
BomEdit.RemoveOperRow = function () {
    var $cbx = $("#tbOperate input:checkbox[name=cbx]:checked:first");
    var tr = $cbx.closest("tr");
    var i = tr.find("#sq").val();
    tr.remove();
    BomEdit.SetSequence();
    BomEdit.settings.currentTr = $("#tbOperate tr:eq(" + (parseInt(i) - 1) + ")");
    BomEdit.settings.currentTr.css('background-color', '#0092DC').siblings().css('background-color', 'white');
    BomEdit.settings.currentTr.find("input[name=cbx]").prop("checked", true);
    BomEdit.settings.currentTr.siblings().find("input[name=cbx]").prop("checked", false);
}

//插入行
BomEdit.InsertOperRow = function () {
    var $cbx = $("#tbOperate input:checkbox[name=cbx]:checked:first");
    var tr = $cbx.closest("tr");

    tr.after(BomEdit.GenerateOneRow(BomEdit.settings.ColumnsParam));
    BomEdit.SetSequence();
    tr.next().find("input[name=MaState]").combobox({
        data: StaticCombo.MaState,
        valueField: 'value',
        textField: 'text',
        panelHeight: "auto",
        editable: true,
        onSelect: function () {
            GenCalculate(this);
        }
    });


    tr.next().find("input[name=IsSubmitMs]").val("Y");
    tr.next().find("input[name=IsSubmitMs]").combobox({
        data: StaticCombo.IsElect,
        valueField: 'value',
        textField: 'text',
        panelHeight: "auto",

    });
    tr.next().find("input[name=IsSubmitMp]").val("Y");
    tr.next().find("input[name=IsSubmitMp]").combobox({
        data: StaticCombo.IsElect,
        valueField: 'value',
        textField: 'text',
        panelHeight: "auto",

    });

    tr.next().find("input[name=IsFixedSize]").combobox({
        data: StaticCombo.IsFixedSize,
        valueField: 'value',
        textField: 'text',
        onLoadSuccess: function () {
            tr.next().find("#IsFixedSize").combobox("setValue", "N");
        },
        onSelect: function () {
            //  alert("aa");
            GenCalculate(this);
        },
        panelHeight: "auto",
        editable: false
    });

    tr.next().find("input[name=Process]").combobox({
        data: StaticCombo.Process,
        valueField: 'value',
        textField: 'text',
        panelHeight: "auto",
        editable: false,
        onSelect: function () {
            var pro = BomEdit.settings.currentTr.find("#Process").combobox("getValue");
            if (pro == '0' || pro == '1' || pro == '2' || pro == '3') {
                BomEdit.settings.currentTr.find("#IsSubmitMs").combobox("select", "Y")
            }
        }
    });

    tr.next().find("input[name=Ku]").combobox({
        data: StaticCombo.Ku,
        valueField: 'value',
        textField: 'text',
        panelHeight: "auto",
        editable: false,
        onSelect: function () {
            var Ku = BomEdit.settings.currentTr.find("#Ku").combobox("getValue");
            if (Ku == 'K') {
                BomEdit.settings.currentTr.find("#IsSubmitMs").combobox("select", "Y")
            }
        }
    });

    tr.next().find("input[name=KeyPart]").combobox({
        data: StaticCombo.KeyPart,
        valueField: 'value',
        textField: 'text',
        onLoadSuccess: function () {
            tr.next().find("#KeyPart").combobox("select", "N");
        },
        panelHeight: "auto",
        editable: false
    });

    tr.next().find("input[name=BlankType]").combobox({
        data: StaticCombo.BlankType,
        valueField: 'value',
        textField: 'text',
        panelHeight: "auto",
        editable: false
    });


    tr.next().find("input[name=MaterialCode]").combobox({
        delay: 500,
        mode: 'remote',
        //   panelWidth: 600,
        panelWidth: "400",
        hasDownArrow: false,
        url: '/Logic/BD/MaterialAjax.aspx?method=GetBomMaterialByCombo',
        queryParams: { EquipId: BomEdit.settings.EquipId, EquipModel: BomEdit.settings.EquipModel },
        onChange: function (newValue, oldValue) {
            BomEdit.settings.currentTr = $(this).closest("tr");
            var text = BomEdit.settings.currentTr.find("#MaterialCode").combobox("getText");
            var maArr = text.split('|');
            if (maArr.length <=11) {
                var r = { Material: maArr[0], Name: maArr[1], Guige: maArr[2], Caizhi: maArr[4], GB: maArr[5], PurcUnit: maArr[6], FuzhuUnit: maArr[7], Mweight: maArr[8], MaState: maArr[9],AreaPerDun: maArr[10] };
                var row = BomEdit.GetData(BomEdit.settings.currentTr);
                BomEdit.CheckComboGrid(row, r);
                BomEdit.LoadData(row, BomEdit.settings.currentTr);
            }
            else if (newValue == "") {
                var r = { Material: "", Name: "", Guige: "", Caizhi: "", GB: "", PurcUnit: "", FuzhuUnit: "", Mweight: "", MaState: "" ,AreaPerDun:""};
                var row = BomEdit.GetData(BomEdit.settings.currentTr);
                BomEdit.CheckComboGrid(row, r);
                BomEdit.LoadData(row, BomEdit.settings.currentTr);
            }
        }
    });

    tr.next().find("#MaterialCode").next().find("input").keyup(function () {
        commonKeyUp(this);
    });
    tr.next().find("#MaterialCode").next().find("input").click(function () {
        CheckAll(this);
    });
}

BomEdit.TrClick = function (input) {
    BomEdit.settings.currentTr = $(input);

    $(input).css('background-color', '#0092DC').siblings().css('background-color', 'white');
    $(input).find("input[name=cbx]").prop("checked", true);
    $(input).siblings().find("input[name=cbx]").prop("checked", false);
    //if (BomEdit.settings.Action == "add") {
    //    var parent = $("#tbList").datagrid("getSelected");
    //    if (parent) {
    //        var parentId = parent.ID;
    //        var parentName = parent.MaterialName;
    //    }
    //    else {
    //        var parentId = 0;
    //        var parentName = "";
    //    }
    //    $(input).find("#ParentId").val(parentId);
    //    $(input).find("#ParentName").val(parentName);
    //    $(input).find("#TaskId").val(BomEdit.settings.TaskId);
    //    $(input).find("#TaskCode").val(BomEdit.settings.TaskCode);
    //}

}


//字段改变是触发的通用计算事件
function GenCalculate(input) {
    if (event && event.keyCode && (event.keyCode == 38 || event.keyCode == 40 || event.keyCode == 39 || event.keyCode == 37 || event.keyCode == 9)) {
        BomEdit.JumpTr(input)
    } else {
        //  BomEdit.settings.currentTr = $(input).closest("tr");
        var taiShu = parseInt($("#lblNum").html());
        if (BomEdit.settings.currentTr != null) {
            var rowData = BomEdit.GetData(BomEdit.settings.currentTr);
            BomEdit.GetXiShu(rowData);
            if (isNaN(parseInt(rowData.SingleNum))) {
                rowData.SingleNum = 0;
                rowData.Num = 0;
            }
            else {
                rowData.Num = rowData.SingleNum * taiShu;
            }
            if (isNaN(parseFloat(rowData.Length))) {
                rowData.Length = 0;
            }
            if (isNaN(parseFloat(rowData.Width))) {
                rowData.Width = 0;
            }

            if (!isNaN(parseFloat(rowData.MaterialTheroWeight))) {

                if (isNaN(parseFloat(rowData.Area))) {
                    rowData.Area = 0;
                } if (isNaN(parseFloat(rowData.PlanArea))) {
                    rowData.PlanArea = 0;
                }
                if (parseFloat(rowData.Length) > 20000) {
                    $.messager.show({
                        title: '警告',
                        msg: '本行长度超过20米，请仔细核对',
                        showType: 'show',
                        style: {
                            right: '',
                            top: document.body.scrollTop + document.documentElement.scrollTop,
                            bottom: ''
                        }
                    });

                }

                if (parseFloat(rowData.Width) > 20000) {
                    $.messager.show({
                        title: '警告',
                        msg: '本行宽度超过20米，请仔细核对',
                        showType: 'show',
                        style: {
                            right: '',
                            top: document.body.scrollTop + document.documentElement.scrollTop,
                            bottom: ''
                        }
                    });
                }
                if (parseFloat(rowData.Num) > 200) {
                    $.messager.show({
                        title: '警告',
                        msg: '本行数量大于200，请仔细核对',
                        showType: 'show',
                        style: {
                            right: '',
                            top: document.body.scrollTop + document.documentElement.scrollTop,
                            bottom: ''
                        }
                    });
                }


                if (rowData.MaState == "板") {

                    if (rowData.Length != 0 && rowData.Length != "" && rowData.Width != "" && rowData.Width != 0) {
                        rowData.MaterialGuige = rowData.HidGuige + 'x' + parseFloat(rowData.Width) + '+' + parseFloat(rowData.Length);
                        rowData.Area = (parseFloat(rowData.Length) * parseFloat(rowData.Width) / 1000000).toFixed(4);
                    }
                    rowData.PlanArea = (parseFloat(rowData.Area) * rowData.XiShu * rowData.Num).toFixed(4);
                    if (rowData.MaterialName.indexOf("钢格板") > -1 || rowData.MaterialName.indexOf("栅格板") > -1 || rowData.MaterialName.indexOf("花纹板") > -1 || rowData.MaterialName.indexOf("钢板网") > -1 || rowData.MaterialName.indexOf("电焊网") > -1) {
                        rowData.MaUnitWeight = (parseFloat(rowData.Area) * parseFloat(rowData.MaterialTheroWeight)).toFixed(4);
                    }
                    else if (rowData.MaterialName.indexOf("复合板") > -1 || rowData.MaterialName.indexOf("复合钢板") > -1 || rowData.MaterialName.indexOf("堆焊板") > -1) {
                        var afterHidGuige = parseFloat(rowData.HidGuige.split('+')[0]) + parseFloat(rowData.HidGuige.split('+')[1]);
                        rowData.MaUnitWeight = (parseFloat(rowData.Area) * parseFloat(afterHidGuige) * parseFloat(rowData.MaterialTheroWeight)).toFixed(4);
                    }
                    else {
                        rowData.MaUnitWeight = (parseFloat(rowData.Area) * parseFloat(rowData.HidGuige) * parseFloat(rowData.MaterialTheroWeight)).toFixed(4);
                    }
                    rowData.MaTotalWeight = (parseFloat(rowData.MaUnitWeight) * parseInt(rowData.Num) * rowData.XiShu).toFixed(4);
                }
                else if (rowData.MaState == "型" || rowData.MaState == "圆钢" || rowData.PurchaseUnit == "米" || rowData.PurchaseUnit == "M" || rowData.PurchaseUnit == "m") {
                    if (rowData.Length != "") {
                        if (rowData.Length != 0) {
                            rowData.MaterialGuige = rowData.HidGuige + '+' + parseFloat(rowData.Length);
                            rowData.TotalLength = (rowData.Length * rowData.Num * rowData.XiShu / 1000).toFixed(4);
                        }
                        else {
                            rowData.TotalLength = parseFloat(rowData.TotalLength).toFixed(4);
                        }
                        if (rowData.TotalLength != 0) {
                            rowData.MaUnitWeight = (rowData.Length * rowData.MaterialTheroWeight / 1000).toFixed(4);
                        }
                        else {
                            rowData.MaUnitWeight = parseFloat(rowData.MaUnitWeight).toFixed(4);
                        }
                        rowData.MaTotalWeight = (rowData.MaUnitWeight * rowData.Num * rowData.XiShu).toFixed(4);
                        if (rowData.MaterialName.indexOf("圆钢")>-1 || rowData.MaterialName.indexOf("管")>-1)
                        {
                            if (rowData.UnitWeight == null || rowData.UnitWeight == 0) {
                                rowData.UnitWeight = parseFloat(rowData.MaUnitWeight).toFixed(4);
                            }
                            else {
                                if (input.id != "SingleNum") {
                                    rowData.UnitWeight = parseFloat(rowData.MaUnitWeight).toFixed(4);
                                }
                            }
                            if (rowData.HidGuige.indexOf("*") > -1) {
                                var d = rowData.HidGuige.split(/[φ *]/);
                               
                                rowData.Area = (parseFloat(rowData.TotalLength) * parseFloat(3.14) * parseFloat(d[1]) / 1000).toFixed(4);
                            } else {
                                var d = rowData.HidGuige.split('φ');
                                rowData.Area = (parseFloat(rowData.TotalLength) * parseFloat(3.14) * parseFloat(d[1]) / 1000).toFixed(4);
                            }
                        } else
                        {
                            if (rowData.UnitWeight == null || rowData.UnitWeight == 0) {
                                rowData.UnitWeight = parseFloat(rowData.MaUnitWeight).toFixed(4);
                            }
                            else {
                                if (input.id != "SingleNum") {
                                    rowData.UnitWeight = parseFloat(rowData.MaUnitWeight).toFixed(4);
                                }
                            }
                            rowData.Area = (rowData.UnitWeight * rowData.Num * rowData.AreaPerDun / 1000).toFixed(4);
                        }
                       
                    }
                }
                    //个 块 米 平米 立方米 根 卷 盘 台 套 支 对 片 件 升 毫升 把 包 本 袋 付 管 罐 盒 节 瓶 双 箱 条 组 桶
                    // && (rowData.PurchaseUnit == "个" || rowData.PurchaseUnit == "套" || rowData.PurchaseUnit == "台" || rowData.PurchaseUnit == "片" || rowData.PurchaseUnit == "件" || rowData.PurchaseUnit == "只")
                else if ((rowData.MaState == "标" || rowData.MaState == "协A" || rowData.MaState == "协B" || rowData.MaState == "成交")) {
                    if (rowData.MaterialName.indexOf("钢格板") > -1 || rowData.MaterialName.indexOf("栅格板") > -1 || rowData.MaterialName.indexOf("花纹板") > -1 || rowData.MaterialName.indexOf("钢板网") > -1 || rowData.MaterialName.indexOf("电焊网") > -1) {

                        if (rowData.Length != 0 && rowData.Length != "" && rowData.Width != "" && rowData.Width != 0) {
                            rowData.MaterialGuige = rowData.HidGuige + 'x' + parseFloat(rowData.Width) + '+' + parseFloat(rowData.Length);
                            rowData.Area = (parseFloat(rowData.Length) * parseFloat(rowData.Width) / 1000000).toFixed(4);
                        }
                        else {
                            rowData.Area = 0;
                        }
                        rowData.PlanArea = (parseFloat(rowData.Area) * rowData.XiShu * rowData.Num).toFixed(4);
                        rowData.MaUnitWeight = (parseFloat(rowData.Area) * parseFloat(rowData.MaterialTheroWeight)).toFixed(4);
                    }
                    else if (rowData.MaterialName.indexOf("复合板") > -1 || rowData.MaterialName.indexOf("复合钢板") > -1 || rowData.MaterialName.indexOf("堆焊板") > -1) {
                        if (rowData.Length != 0 && rowData.Length != "" && rowData.Width != "" && rowData.Width != 0) {
                            rowData.MaterialGuige = rowData.HidGuige + 'x' + parseFloat(rowData.Width) + '+' + parseFloat(rowData.Length);
                            rowData.Area = (parseFloat(rowData.Length) * parseFloat(rowData.Width) / 1000000).toFixed(4);
                        }
                        rowData.PlanArea = (parseFloat(rowData.Area) * rowData.XiShu * rowData.Num).toFixed(4);
                        var afterHidGuige = parseFloat(rowData.HidGuige.split('+')[0]) + parseFloat(rowData.HidGuige.split('+')[1]);
                        rowData.MaUnitWeight = (parseFloat(rowData.Area) * parseFloat(afterHidGuige) * parseFloat(rowData.MaterialTheroWeight)).toFixed(4);
                    }
                    else {
                        rowData.MaUnitWeight = parseFloat(rowData.MaterialTheroWeight).toFixed(4);
                    }
                }
                if (rowData.PurchaseUnit == "m" || rowData.PurchaseUnit == "M" || rowData.PurchaseUnit == "米") {
                    rowData.TotalLength = (rowData.Length * rowData.Num * rowData.XiShu / 1000).toFixed(4);
                }
                rowData.MaTotalWeight = (parseFloat(rowData.MaUnitWeight) * rowData.Num * rowData.XiShu).toFixed(4);
                if (rowData.UnitWeight == null || rowData.UnitWeight == 0) {
                    rowData.UnitWeight = parseFloat(rowData.MaUnitWeight).toFixed(4);
                }
                else {
                    if (input.id != "SingleNum") {
                        rowData.UnitWeight = parseFloat(rowData.MaUnitWeight).toFixed(4);
                    }
                }
                rowData.TotalWeight = (rowData.UnitWeight * rowData.Num).toFixed(4);

                if (!rowData.MaterialCode == "" || !rowData.MaterialName == "") {
                    if (rowData.UnitWeight == "0.0000") {

                        rowData.UnitWeight = "0";
                    }
                    if (rowData.TotalWeight == "0.0000") {
                        rowData.TotalWeight = "0";
                    }
                    if (rowData.MaUnitWeight == "0.0000") {
                        rowData.MaUnitWeight = "0";
                    }
                    if (rowData.MaTotalWeight == "0.0000") {
                        rowData.MaTotalWeight = "0";
                    }
                }

            }
            BomEdit.LoadData(rowData, BomEdit.settings.currentTr);
        }
    }
}
//物料单重改变
function WeightChange(input) {
    if (event && event.keyCode && (event.keyCode == 38 || event.keyCode == 40 || event.keyCode == 39 || event.keyCode == 37 || event.keyCode == 9)) {
        BomEdit.JumpTr(input)
    } else {
        BomEdit.settings.currentTr = $(input).closest("tr");
        var rowData = BomEdit.GetData(BomEdit.settings.currentTr);
        //类型检查
        if (isNaN(parseFloat(rowData.UnitWeight))) {
            rowData.UnitWeight = 0;
        }
        if (isNaN(parseInt(rowData.Num))) {
            rowData.Num = 0;
        }
        rowData.TotalWeight = parseFloat(rowData.UnitWeight) * parseInt(rowData.Num).toFixed(4);
        CalculateArea(rowData);
        BomEdit.LoadData(rowData, BomEdit.settings.currentTr);
    }
}
//材料单重改变事件
function MaWeightChange(input) {
    if (event && event.keyCode && (event.keyCode == 38 || event.keyCode == 40 || event.keyCode == 39 || event.keyCode == 37 || event.keyCode == 9)) {
        BomEdit.JumpTr(input)
    } else {
        BomEdit.settings.currentTr = $(input).closest("tr");
        var rowData = BomEdit.GetData(BomEdit.settings.currentTr);
        BomEdit.GetXiShu(rowData);
        //类型检查
        if (isNaN(parseFloat(rowData.MaUnitWeight))) {
            rowData.UnitWeight = 0;
        }
        if (isNaN(parseInt(rowData.Num))) {
            rowData.Num = 0;
        }
        if (isNaN(parseFloat(rowData.MaUnitWeight))) {
            rowData.MaUnitWeight = 0;
        }

        rowData.MaUnitWeight = rowData.MaUnitWeight;
        rowData.MaTotalWeight = (parseFloat(rowData.MaUnitWeight) * parseInt(rowData.Num) * parseFloat(rowData.XiShu)).toFixed(4);
        rowData.UnitWeight = parseFloat(rowData.MaUnitWeight).toFixed(4);
        rowData.TotalWeight = (parseFloat(rowData.MaUnitWeight) * parseFloat(rowData.Num)).toFixed(4);
        // rowData.Area = (parseFloat(rowData.MaUnitWeight) * parseFloat(rowData.Num) * parseFloat(rowData.AreaPerDun) / 1000).toFixed(4);
          CalculateArea(rowData);
        BomEdit.LoadData(rowData, BomEdit.settings.currentTr);
    }
}


//function ChangeArea(input) {
//    if (event && event.keyCode && (event.keyCode == 38 || event.keyCode == 40 || event.keyCode == 39 || event.keyCode == 37 || event.keyCode == 9)) {
//        BomEdit.JumpTr(input)
//    } else {
//        BomEdit.settings.currentTr = $(input).closest("tr");
//        var rowData = BomEdit.GetData(BomEdit.settings.currentTr);
//        BomEdit.GetXiShu(rowData);
//        rowData.PlanArea = utils.ParseFloat(rowData.Area) * rowData.Num * rowData.XiShu;
//        if (rowData.MaState == "板") {
//            if (utils.ParseFloat(rowData.HidGuige) != 0) {
//                rowData.MaUnitWeight = (utils.ParseFloat(rowData.Area) * utils.ParseFloat(rowData.MaterialTheroWeight) * parseFloat(rowData.MaterialTheroWeight)).toFixed(4);
//                rowData.MaTotalWeight = (parseFloat(rowData.MaUnitWeight) * parseInt(rowData.Num) * parseFloat(rowData.XiShu)).toFixed(4);
//                rowData.UnitWeight = parseFloat(rowData.MaUnitWeight).toFixed(4);
//                rowData.TotalWeight = (parseFloat(rowData.MaUnitWeight) * parseFloat(rowData.Num)).toFixed(4);
//            }
//        }

//        BomEdit.LoadData(rowData, BomEdit.settings.currentTr);
//    }


//}

function CalculateArea(rowData) {
    if (rowData.MaState == "板") {
        if (utils.ParseFloat(rowData.HidGuige) != 0 && utils.ParseFloat(rowData.MaterialTheroWeight) != 0) {
            rowData.Area = (rowData.UnitWeight / utils.ParseFloat(rowData.HidGuige) / rowData.MaterialTheroWeight).toFixed(4);
            rowData.PlanArea = (rowData.Area * utils.ParseFloat(rowData.XiShu)).toFixed(4);
        }
        else {
            rowData.Area = 0;
            rowData.PlanArea = 0;
        }
    }else if(rowData.MaState=="型"){
        if (rowData.MaterialName.indexOf("圆钢") > -1 || rowData.MaterialName.indexOf("管") > -1) {
            if (rowData.HidGuige.indexOf("*") > -1) {
                var d = rowData.HidGuige.split('φ');
                var s = d.split('*');
                rowData.Area = (parseFloat(rowData.TotalLength) * parseFloat(3.14) * parseFloat(s[0]) / 1000).toFixed(4);
            } else {
                var d = rowData.HidGuige.split('φ');
                rowData.Area = (parseFloat(rowData.TotalLength) * parseFloat(3.14) * parseFloat(d[0]) / 1000).toFixed(4);
            }
        } else {
            rowData.Area = (parseFloat(rowData.UnitWeight) * parseFloat(rowData.Num) * parseFloat(rowData.AreaPerDun) / 1000).toFixed(4);
        }
    }
    else {
        rowData.Area = 0;
        rowData.PlanArea = 0;
    }
}

//function ChangeXuhao(input) {
//    if (event && event.keyCode && (event.keyCode == 38 || event.keyCode == 40 || event.keyCode == 39 || event.keyCode == 37 || event.keyCode == 9)) {
//        commonKeyDown(input);
//    } else {

//        BomEdit.settings.currentTr = $(input).closest("tr");
//        var XuhaoModel = $("#XuhaoModel").combobox("getValue");
//        if (XuhaoModel == "0") {
//            alert($(input).val());
//            BomEdit.settings.currentTr.find("#Zongxu").val($(input).val())
//        }
//    }
//}

function CheckAll(input) {
    $(input).select();

}

function commonKeyUp(input) {
    BomEdit.JumpTr(input);
}

function commonKeyDown(input) {
    if (BomEdit.settings.currentTr != null) {
        var targetTr = undefined;
        if (event && event.keyCode && (event.keyCode == 38 || event.keyCode == 40)) {

            if (event && event.keyCode && event.keyCode == 38) {
                targetTr = BomEdit.settings.currentTr.prev();
                var inputId = input.id;
            }
            if (event && event.keyCode && event.keyCode == 40) {
                targetTr = BomEdit.settings.currentTr.next();
                var inputId = input.id;
            }

            if (targetTr && targetTr.find("#cbx").length > 0) {

                targetTr.find("#" + inputId).focus();


                BomEdit.settings.currentTr = targetTr;

                BomEdit.settings.currentTr.css('background-color', '#0092DC').siblings().css('background-color', 'white');
                BomEdit.settings.currentTr.find("input[name=cbx]").prop("checked", true);
                BomEdit.settings.currentTr.siblings().find("input[name=cbx]").prop("checked", false);

            }

            var inputId = input.id;
            if (event.keyCode == 40 && inputId == "Xuhao") {
                if (BomEdit.settings.currentTr.find("#Xuhao").val() == null || BomEdit.settings.currentTr.find("#Xuhao").val() == "") {
                    var lastXuhao = BomEdit.settings.currentTr.prev().find("#Xuhao").val();
                    var autoXuhao = $("#autoXuhao").combobox("getValue");
                    if (autoXuhao == "0") {
                        var newXuhao = "";
                        if (lastXuhao.lastIndexOf(".") == -1) {
                            newXuhao = utils.ParseFloat(lastXuhao) + 1;
                        }
                        else {
                            var fXuhao = lastXuhao.substring(0, lastXuhao.lastIndexOf("."));
                            var subXuhao = lastXuhao.split('.')[lastXuhao.split('.').length - 1];
                            newXuhao = fXuhao + '.' + (utils.ParseFloat(subXuhao) + 1);

                        }
                        BomEdit.settings.currentTr.find("#Xuhao").val(newXuhao);

                    }
                    else if (autoXuhao == "1") {
                        var frontXuhao = $("#frontXuhao").val()
                        var newXuhao = frontXuhao + "." + lastXuhao;
                        BomEdit.settings.currentTr.prev().find("#Xuhao").val(newXuhao);
                    }
                    var XuhaoModel = $("#XuhaoModel").combobox("getValue");
                    if (XuhaoModel == "0") {
                        if (autoXuhao == "0") {
                            BomEdit.settings.currentTr.find("#Zongxu").val(newXuhao);
                        }
                        else if (autoXuhao == "1") {
                            BomEdit.settings.currentTr.prev().find("#Zongxu").val(newXuhao);
                        }
                    }
                }
            }
            if (event.keyCode == 40 && inputId == "MsXuhao" && $("#autoMsXuhao").combobox("getValue") == "0") {

                var lastMsXuhao = BomEdit.settings.currentTr.prev().find("#MsXuhao").val();

                if (lastMsXuhao.lastIndexOf(".") == -1) {
                    newXuhao = utils.ParseFloat(lastMsXuhao) + 1;
                }
                else {
                    var fXuhao = lastMsXuhao.substring(0, lastMsXuhao.lastIndexOf("."));
                    var subXuhao = lastMsXuhao.split('.')[lastMsXuhao.split('.').length - 1];
                    var newXuhao = fXuhao + '.' + (utils.ParseFloat(subXuhao) + 1);
                }
                BomEdit.settings.currentTr.find("#MsXuhao").val(newXuhao);

            }
            if (event.keyCode == 40 && inputId == "Map") {
                if (BomEdit.settings.currentTr.find("#Map").val() == "") {


                    if ($("#autoMap").combobox("getValue") == "1") {
                        var lastMap = BomEdit.settings.currentTr.prev().find("#Map").val();
                        BomEdit.settings.currentTr.find("#Map").val(lastMap);
                    }
                    else if ($("#autoMap").combobox("getValue") == "2") {
                        var frontMap = $("#frontMap").val();
                        var lastMap = BomEdit.settings.currentTr.prev().find("#Map").val();
                        var num = lastMap.replace(frontMap, "");
                        var newMap = frontMap + (utils.ParseInt(num) + 1);
                        BomEdit.settings.currentTr.find("#Map").val(newMap);
                    }
                    else if ($("#autoMap").combobox("getValue") == "3") {
                        var frontMap = $("#frontMap").val();
                        var lastMap = BomEdit.settings.currentTr.prev().find("#Map").val();
                        var newMap = frontMap + lastMap;
                        BomEdit.settings.currentTr.prev().find("#Map").val(newMap);
                    }
                }
            }
        } else if (event && event.keyCode && (event.keyCode == 37 || event.keyCode == 39)) {
            var $prevInput = undefined;
            if (event.keyCode == 37) {
                var $prevTd = $(input).closest("td").prev();
                while (true) {
                    if ($prevTd.css("display") != "none") {
                        var subInput = $prevTd.find("input");
                        if (subInput.length > 0) {
                            for (var i = 0; i < subInput.length; i++) {

                                if ($(subInput[i]).css("display") != "none" && typeof ($(subInput[i]).attr("readonly")) == "undefined") {
                                    $(subInput[i]).focus();
                                    //  prevInput.select();
                                    // $prevTd.find("input").select();
                                    return;
                                }
                            }
                            $prevTd = $prevTd.prev();
                        }
                        else {
                            $prevTd = $prevTd.prev();
                        }
                    }
                    else {
                        $prevTd = $prevTd.prev();
                    }
                }

            }
            else if (event.keyCode == 39) {
                var $prevTd = $(input).closest("td").next();
                while (true) {

                    if ($prevTd.css("display") != "none") {
                        if ((typeof ($prevTd.find("input").attr("readonly")) == "undefined") && ($prevTd.find("input").css("display") != "none")) {
                            $prevTd.find("input").focus();
                            // $prevTd.find("input").select();
                            break;
                        }
                        else {
                            $prevTd = $prevTd.next();
                        }
                    }
                    else {
                        $prevTd = $prevTd.next();
                    }
                }
            }
        }
    }
}

function ClickJumpTr(input) {

    BomEdit.settings.currentTr = $(input).closest("tr");

    var prevTr = $(input).closest("tr").prev();
    var lastXuhao = prevTr.find("#Xuhao").val();
    var newXuhao = "";
    if ($(input).val() == null || $(input).val() == "") {
        if (lastXuhao) {
            if (lastXuhao.lastIndexOf(".") == -1) {
                newXuhao = utils.ParseFloat(lastXuhao) + 1;
            }
            else {
                var fXuhao = lastXuhao.substring(0, lastXuhao.lastIndexOf("."));
                var subXuhao = lastXuhao.split('.')[lastXuhao.split('.').length - 1];
                newXuhao = fXuhao + '.' + (utils.ParseFloat(subXuhao) + 1);
            }
            $(input).val(newXuhao);
            var XuhaoModel = $("#XuhaoModel").combobox("getValue");
            if (XuhaoModel == "0") {
                BomEdit.settings.currentTr.find("#Zongxu").val(newXuhao);
            }
        }
    }
    CheckAll(input);
}

BomEdit.JumpTr = function (input) {

    if (BomEdit.settings.currentTr != null) {
        if (event && event.keyCode && (event.keyCode == 38 || event.keyCode == 40 || event.keyCode == 37 || event.keyCode == 39)) {
            if (BomEdit.settings.currentTr != null) {
                $(input).select();
            }
        }
        else {
            if (input.id == "Xuhao") {
                var XuhaoModel = $("#XuhaoModel").combobox("getValue");
                if (XuhaoModel == "0") {

                    BomEdit.settings.currentTr.find("#Zongxu").val($(input).val())
                }
            }
        }
    }
}

BomEdit.GenerateOneRow = function (columnData) {
    var $tr = $("<tr onclick='BomEdit.TrClick(this)'></tr>");
    for (var j = 0; j < columnData.length; j++) {
        var display = "";
        if (columnData[j].IsHidden == "1") {
            display = ";display:none";
        }
        var formatter = "";
        var keyup = "";
        var keydown = "";
        var click = "";
        var ondbClick = "";

        var readOnly = "";
        if (columnData[j].ReadOnly == "0") {
            readOnly = "readonly ='true'";
        }
        if (columnData[j].OnKeyUp != "") {
            keyup = "onkeyup='" + columnData[j].OnKeyUp + "(this)'";
        }
        if (columnData[j].OnKeyDown != "") {
            keydown = "onkeydown='" + columnData[j].OnKeyDown + "(this)'";
        }
        if (columnData[j].OnClick != "") {
            click = "onClick='" + columnData[j].OnClick + "(this)'";
        }
        if (columnData[j].OnDoubleClick != "") {
            ondbClick = "ondblclick='" + columnData[j].OnDoubleClick + "(this)'";
        }
        //ClickJumpTr

        if (columnData[j].Formatter == "text") {
            formatter = "<input type='text' id='" + columnData[j].Field + "'  " + keyup + "   " + keydown + "  " + click + " " + ondbClick + " " + readOnly + " name='" + columnData[j].Field + "' style='width:" + (parseInt(columnData[j].Length) - 10) + "px'/>"
        }
        else if (columnData[j].Formatter == "checkbox") {
            formatter = "<input type='checkbox' id='" + columnData[j].Field + "' name='" + columnData[j].Field + "'/><input id='sq' name='sq' readonly='readonly' style='width:20px'>"
        }
        else if (columnData[j].Formatter == "special") {
            formatter = "<input  id='" + columnData[j].Field + "' " + keyup + "   " + keydown + "  name='" + columnData[j].Field + "'  style='width:" + (parseInt(columnData[j].Length) - 10) + "px' />"
        }
        var td = $(" <td style='text-align:center" + display + "'><div style='width:" + columnData[j].Length + "px;'>" + formatter + "</div></td>");

        $tr.append(td);
    }
    var hidtd = $("<td style='display:none'></td>");
    var hidGuige = $("<input type='hidden' id='HidGuige' name='HidGuige'/>");
    hidtd.append(hidGuige);
    var hidMpState = $("<input type='hidden' id='MpState' name='MpState'/>");
    hidtd.append(hidMpState);
    var hidMsState = $("<input type='hidden' id='MsState' name='MsState'/>");
    hidtd.append(hidMsState);
    var hidOsState = $("<input type='hidden' id='OsState' name='OsState'/>");
    hidtd.append(hidOsState);
    var hidJsState = $("<input type='hidden' id='JsState' name='JsState'/>");
    hidtd.append(hidJsState);
    var hidMkState = $("<input type='hidden' id='MkState' name='MkState'/>");
    hidtd.append(hidMkState);
    var hidMapAssState = $("<input type='hidden' id='MapAssState' name='MapAssState'/>");
    hidtd.append(hidMapAssState);
    var hidMpReState = $("<input type='hidden' id='MpReviewState' name='MpReviewState'/>");
    hidtd.append(hidMpReState);
    var hidMsReState = $("<input type='hidden' id='MsReviewState' name='MsReviewState'/>");
    hidtd.append(hidMsReState);
    var hidOsReState = $("<input type='hidden' id='OsReviewState' name='OsReviewState'/>");
    hidtd.append(hidOsReState);
    var hidJsReState = $("<input type='hidden' id='JsReviewState' name='JsReviewState'/>");
    hidtd.append(hidJsReState);
    var hidMkReState = $("<input type='hidden' id='MkReviewState' name='MkReviewState'/>");
    hidtd.append(hidMkReState);
    var hidClassId = $("<input type='hidden' id='ClassId' name='ClassId' value='0'/>");
    hidtd.append(hidClassId);
    var hidClassName = $("<input type='hidden' id='ClassName' name='ClassName'/>");
    hidtd.append(hidClassName);
    var hidXiShu = $("<input type='hidden' id='XiShu' name='XiShu'/>");
    hidtd.append(hidXiShu);
    $tr.append(hidtd);
    return $tr;
}
//初始化操作列表，插入行
BomEdit.AddOperationRow = function (num, columnData) {
    for (var i = 0; i < num; i++) {
        var tr = BomEdit.GenerateOneRow(columnData)
        tr.find("input[name=MaState]").combobox({
            data: StaticCombo.MaState,
            valueField: 'value',
            textField: 'text',
            panelHeight: "auto",
            editable: true,
            onSelect: function () {
                GenCalculate(this);
            }
        });

        tr.find("input[name=IsFixedSize]").combobox({
            data: StaticCombo.IsFixedSize,
            valueField: 'value',
            textField: 'text',
            onLoadSuccess: function () {

                tr.find("#IsFixedSize").combobox("setValue", "N");
            },
            onSelect: function () {
                //  alert("aa");
                GenCalculate(this);
            },
            panelHeight: "auto",
            editable: false
        });

        tr.find("input[name=IsSubmitMs]").val("Y");
        tr.find("input[name=IsSubmitMs]").combobox({
            data: StaticCombo.IsElect,
            valueField: 'value',
            textField: 'text',
            panelHeight: "auto",

        });
        tr.find("input[name=IsSubmitMp]").val("Y");
        tr.find("input[name=IsSubmitMp]").combobox({
            data: StaticCombo.IsElect,
            valueField: 'value',
            textField: 'text',
            panelHeight: "auto",

        });

        tr.find("input[name=Process]").combobox({
            data: StaticCombo.Process,
            valueField: 'value',
            textField: 'text',
            panelHeight: "auto",
            onSelect: function () {
                var pro = BomEdit.settings.currentTr.find("#Process").combobox("getValue");
                if (pro == '0' || pro == '1' || pro == '2' || pro == '3') {
                    BomEdit.settings.currentTr.find("#IsSubmitMs").combobox("select", "Y")
                }
            }
        });

        tr.find("input[name=Ku]").combobox({
            data: StaticCombo.Ku,
            valueField: 'value',
            textField: 'text',
            panelHeight: "auto",
            onSelect: function () {
                var Ku = BomEdit.settings.currentTr.find("#Ku").combobox("getValue");
                if (Ku == 'K') {
                    BomEdit.settings.currentTr.find("#IsSubmitMs").combobox("select", "Y")
                }
            }

        });


        tr.find("input[name=KeyPart]").combobox({
            data: StaticCombo.KeyPart,
            valueField: 'value',
            textField: 'text',
            onLoadSuccess: function () {
                tr.find("#KeyPart").combobox("select", "N");
            },
            panelHeight: "auto",
            editable: false
        });

        tr.find("input[name=BlankType]").combobox({
            data: StaticCombo.BlankType,
            valueField: 'value',
            textField: 'text',
            panelHeight: "auto",
          //  editable: false
        });

        tr.find("input[name=MaterialCode]").combobox({
            delay: 500,
            //   panelWidth: 600,
            panelWidth: 400,
            hasDownArrow: false,
            mode: 'remote',
            url: '/Logic/BD/MaterialAjax.aspx?method=GetBomMaterialByCombo',
            queryParams: { EquipId: BomEdit.settings.EquipId, EquipModel: BomEdit.settings.EquipModel },
            // idField: 'Material',
            // textField: 'Material',
            onChange: function (newValue, oldValue) {
                BomEdit.settings.currentTr = $(this).closest("tr");
                var text = BomEdit.settings.currentTr.find("#MaterialCode").combobox("getText");
                var maArr = text.split('|');
                if (maArr.length <= 11) {
                    var r = { Material: maArr[0], Name: maArr[1], Guige: maArr[2], Caizhi: maArr[4], GB: maArr[5], PurcUnit: maArr[6], FuzhuUnit: maArr[7], Mweight: maArr[8], MaState: maArr[9], AreaPerDun: maArr[10] };
                    var row = BomEdit.GetData(BomEdit.settings.currentTr);
                    BomEdit.CheckComboGrid(row, r);
                    BomEdit.LoadData(row, BomEdit.settings.currentTr);
                }
                else if (newValue == "") {
                    var r = { Material: "", Name: "", Guige: "", Caizhi: "", GB: "", PurcUnit: "", FuzhuUnit: "", Mweight: "", MaState: "", AreaPerDun: "" };
                    var row = BomEdit.GetData(BomEdit.settings.currentTr);
                    BomEdit.CheckComboGrid(row, r);
                    BomEdit.LoadData(row, BomEdit.settings.currentTr);
                }
            }
        });
        tr.find("#MaterialCode").next().find("input").keyup(function () {
            commonKeyUp(this);
        });
        tr.find("#MaterialCode").next().find("input").click(function () {
            CheckAll(this);
        });
        $("#tbOperate").append(tr);

    }
}


function ShowMsNote(input) {
    $("#divMainNote").dialog("open");
    $("#mainNote").val($(input).val());

}


//选择物料编码触发事件
BomEdit.CheckComboGrid = function (row, r) {
    row.MaterialCode = r.Material;
    if (r.Material && r.Material.indexOf('*') == -1) {
        row.MaterialName = r.Name;
        row.OrgMaterialName = r.Name;
        row.MaterialGuige = r.Guige;
        row.HidGuige = r.Guige;
        row.MaterialCaizhi = r.Caizhi;
        row.MaterialGB = r.GB;
        row.PurchaseUnit = r.PurcUnit;
        row.AssistUnit = r.FuzhuUnit;
        row.MaterialTheroWeight = r.Mweight;
        row.UnitWeight = "0";
        row.TotalWeight = "0";
        row.SingleNum = "0";
        row.Num = "0";
        row.MaState = r.MaState;
        row.Process = "";
        row.SelfWeight = "";
        row.AreaPerDun=r.AreaPerDun;
        
    }
    else {
        //    row.MaterialName = "";
        row.PartName = r.Name;
        row.MaterialGuige = "";
        row.HidGuige = "";
        row.MaterialCaizhi = "";
        row.MaterialGB = "";
        row.PurchaseUnit = "";
        row.AssistUnit = "";
        row.MaterialTheroWeight = "";
        row.MaState = "";
        if (isNaN(parseFloat(row.SingleNum))) {
            row.SingleNum = 0;
        }
        if (isNaN(parseFloat(row.Num))) {
            row.Num = 0;
        }
    }
    row.ConditionAttr = "";
    row.Length = "0";
    row.Width = "0";
    row.MaUnitWeight = "0";
    row.MaTotalWeight = "0";
    row.TotalLength = "0";
    row.Area = "0";
    row.IsFixedSize = "N";
    row.PlanArea = "0";
    row.Ku = "";
    row.EngName = "";
    row.XiShu = "1";
    if (r.MaState == "板" || r.MaState == "型") {
        row.IsSubmitMs = "N";
    }
    else {
        row.IsSubmitMs = "Y";
    }

    row.IsSubmitMp = "Y";
    if (r.MaState == "标") {
        row.Note = r.GB
    }
}






BomEdit.LoadData = function (rowData, tr) {
    tr.find("#ID").val(rowData.ID);
    tr.find("#TaskId").val(rowData.TaskId);
    tr.find("#TaskCode").val(rowData.TaskCode);
    tr.find("#Xuhao").val(rowData.Xuhao);
    tr.find("#Zongxu").val(rowData.Zongxu);
    tr.find("#MsXuhao").val(rowData.MsXuhao);
    tr.find("#Map").val(rowData.Map);
    tr.find("#MaterialCode").combobox("setValue", rowData.MaterialCode);
    tr.find("#MaterialName").val(rowData.MaterialName);
    tr.find("#OrgMaterialName").val(rowData.OrgMaterialName);
    tr.find("#MaterialGuige").val(rowData.MaterialGuige);
    tr.find("#MaterialCaizhi").val(rowData.MaterialCaizhi);
    tr.find("#MaterialGB").val(rowData.MaterialGB);
    tr.find("#PurchaseUnit").val(rowData.PurchaseUnit);
    tr.find("#AssistUnit").val(rowData.AssistUnit);
    tr.find("#ConditionAttr").val(rowData.ConditionAttr);
    tr.find("#Length").val(rowData.Length);
    tr.find("#Width").val(rowData.Width);
    tr.find("#MaUnitWeight").val((rowData.MaUnitWeight));
    tr.find("#MaTotalWeight").val((rowData.MaTotalWeight));
    tr.find("#TotalLength").val((rowData.TotalLength));
    tr.find("#Area").val((rowData.Area));
    tr.find("#UnitWeight").val((rowData.UnitWeight));
    tr.find("#TotalWeight").val((rowData.TotalWeight));
    tr.find("#SingleNum").val(rowData.SingleNum);
    tr.find("#Num").val(rowData.Num);
    tr.find("#MaState").combobox("setValue", rowData.MaState);
    tr.find("#IsFixedSize").combobox("setValue", rowData.IsFixedSize);
    tr.find("#Note").val(rowData.Note);
    tr.find("#PlanArea").val((rowData.PlanArea));
    tr.find("#Ku").combobox("setValue", rowData.Ku);
    tr.find("#EngName").val(rowData.EngName);
    tr.find("#HidGuige").val(rowData.HidGuige);
    tr.find("#MaterialTheroWeight").val(rowData.MaterialTheroWeight);
    tr.find("#ParentId").val(rowData.ParentId);
    tr.find("#ParentName").val(rowData.ParentName);
    tr.find("#MpState").val(rowData.MpState);
    tr.find("#MsState").val(rowData.MsState);
    tr.find("#OsState").val(rowData.OsState);
    tr.find("#JsState").val(rowData.JsState);
    tr.find("#MkState").val(rowData.MkState);
    tr.find("#MapAssState").val(rowData.MapAssState);
    tr.find("#MpReviewState").val(rowData.MpReviewState);
    tr.find("#MsReviewState").val(rowData.MsReviewState);
    tr.find("#OsReviewState").val(rowData.OsReviewState);
    tr.find("#JsReviewState").val(rowData.JsReviewState);
    tr.find("#MkReviewState").val(rowData.MkReviewState);
    tr.find("#ClassId").val(rowData.ClassId);
    tr.find("#ClassName").val(rowData.ClassName);
    tr.find("#KeyPart").combobox("setValue", rowData.KeyPart);
    tr.find("#BlankType").combobox("setValue", rowData.BlankType);
    tr.find("#SelfWeight").val(rowData.SelfWeight);
    tr.find("#Process").combobox("setValue", rowData.Process);
    tr.find("#IsSubmitMs").combobox("setValue", rowData.IsSubmitMs);
    tr.find("#IsSubmitMp").combobox("setValue", rowData.IsSubmitMp);
    tr.find("#XiShu").val(rowData.XiShu);
    tr.find("#PartName").val(rowData.PartName);
    tr.find("#AreaPerDun").val(rowData.AreaPerDun);
}

BomEdit.GetData = function (tr) {
    var row = {};
    row.ID = tr.find("#ID").val();
    row.TaskId = tr.find("#TaskId").val();
    row.TaskCode = tr.find("#TaskCode").val();
    row.Xuhao = tr.find("#Xuhao").val();
    row.Zongxu = tr.find("#Zongxu").val();
    row.MsXuhao = tr.find("#MsXuhao").val();
    row.Map = tr.find("#Map").val();
    row.MaterialCode = tr.find("#MaterialCode").combobox("getValue");
    row.MaterialName = tr.find("#MaterialName").val();
    row.OrgMaterialName = tr.find("#OrgMaterialName").val();
    row.MaterialGuige = tr.find("#MaterialGuige").val();
    row.MaterialCaizhi = tr.find("#MaterialCaizhi").val();
    row.MaterialGB = tr.find("#MaterialGB").val();
    row.PurchaseUnit = tr.find("#PurchaseUnit").val();
    row.AssistUnit = tr.find("#AssistUnit").val();
    row.ConditionAttr = tr.find("#ConditionAttr").val();
    row.Length = parseFloat(tr.find("#Length").val());
    row.Width = parseFloat(tr.find("#Width").val());
    row.MaUnitWeight = tr.find("#MaUnitWeight").val();
    row.MaTotalWeight = tr.find("#MaTotalWeight").val();
    row.TotalLength = tr.find("#TotalLength").val();
    row.Area = tr.find("#Area").val();
    row.UnitWeight = tr.find("#UnitWeight").val();
    row.TotalWeight = tr.find("#TotalWeight").val();
    row.SingleNum = parseInt(tr.find("#SingleNum").val());
    row.Num = parseInt(tr.find("#Num").val());
    row.MaState = tr.find("#MaState").combobox("getValue");
    row.IsFixedSize = tr.find("#IsFixedSize").combobox("getValue");
    row.Note = tr.find("#Note").val();
    row.PlanArea = tr.find("#PlanArea").val();
    row.Ku = tr.find("#Ku").combobox("getValue");
    row.Process = tr.find("#Process").combobox("getValue");
    row.EngName = tr.find("#EngName").val();
    row.HidGuige = tr.find("#HidGuige").val();
    row.MaterialTheroWeight = tr.find("#MaterialTheroWeight").val();
    row.ParentId = tr.find("#ParentId").val();
    row.ParentName = tr.find("#ParentName").val();
    row.MpState = tr.find("#MpState").val();
    row.MsState = tr.find("#MsState").val();
    row.OsState = tr.find("#OsState").val();
    row.JsState = tr.find("#JsState").val();
    row.MkState = tr.find("#MkState").val();
    row.MapAssState = tr.find("#MapAssState").val();
    row.MpReviewState = tr.find("#MpReviewState").val();
    row.MsReviewState = tr.find("#MsReviewState").val();
    row.OsReviewState = tr.find("#OsReviewState").val();
    row.JsReviewState = tr.find("#JsReviewState").val();
    row.MkReviewState = tr.find("#MkReviewState").val();
    row.ClassId = tr.find("#ClassId").val();
    row.ClassName = tr.find("#ClassName").val();
    row.KeyPart = tr.find("#KeyPart").combobox("getValue");
    row.BlankType = tr.find("#BlankType").combobox("getValue");
    row.SelfWeight = tr.find("#SelfWeight").val();
    row.IsSubmitMs = tr.find("#IsSubmitMs").combobox("getValue");
    row.IsSubmitMp = tr.find("#IsSubmitMp").combobox("getValue");
    row.XiShu = tr.find("#XiShu").val();
    row.PartName = tr.find("#PartName").val();
    row.AreaPerDun = tr.find("#AreaPerDun").val();
    return row;
}

BomEdit.SaveOrgData = function () {
    var array = new Array();
    var $trs = $("#tbOperate tr");
    $trs.each(function () {
        var row = new Object();
        row = BomEdit.GetData($(this));
        //   console.log(row);
        if (BomEdit.settings.Action == "add" || BomEdit.settings.Action == "batch") {
            row.ID = 0;
            row.TaskId = BomEdit.settings.TaskId;
            row.TaskCode = BomEdit.settings.TaskCode;
            row.ParentId = 0;
        }
        if (row.Xuhao != null && row.Xuhao != "") {
            array.push(row);
        }
    });
    //  console.log(array);
    if (BomEdit.settings.Action == "add" || BomEdit.settings.Action == "batch") {
        com.ajax({
            url: "/Logic/TM/OrgDataAjax.aspx?method=AddOrgData", data: { data: JSON.stringify(array) }, success: function (data) {
                com.message(data.state, data.msg);
                if (data.state == "error") {

                    if (utils.ParseInt(data.returnValue) != 0) {

                        BomEdit.settings.currentTr = $("#divTableBody").find("tr:eq(" + (utils.ParseInt(data.returnValue) - 1) + ")");
                        BomEdit.settings.currentTr.css('background-color', '#0092DC').siblings().css('background-color', 'white');
                        BomEdit.settings.currentTr.find("input[name=cbx]").prop("checked", true);
                        BomEdit.settings.currentTr.siblings().find("input[name=cbx]").prop("checked", false);

                    }
                }
                if (data.state == "success") {
                    // top.wrapper.tabCurrentClose();
                    if (BomEdit.settings.Action == "add") {
                        BomEdit.LoadOrgOperTable(BomEdit.settings.ColumnsParam, 20);
                        BomEdit.SetSequence();
                    }
                    else {
                        // top.wrapper.tabCurrentClose();
                        $("#tbList").datagrid("reload");
                    }

                }
            }
        });
    }
    else if (BomEdit.settings.Action == "edit") {
        com.ajax({
            url: "/Logic/TM/OrgDataAjax.aspx?method=EditOrgData", data: { data: JSON.stringify(array), TaskId: BomEdit.settings.TaskId, CopySwitch: BomEdit.settings.CopySwitch }, success: function (data) {
                com.message(data.state, data.msg);
                if (data.state == "error") {

                    if (utils.ParseInt(data.returnValue) != 0) {

                        BomEdit.settings.currentTr = $("#divTableBody").find("tr:eq(" + (utils.ParseInt(data.returnValue) - 1) + ")");
                        BomEdit.settings.currentTr.css('background-color', '#0092DC').siblings().css('background-color', 'white');
                        BomEdit.settings.currentTr.find("input[name=cbx]").prop("checked", true);
                        BomEdit.settings.currentTr.siblings().find("input[name=cbx]").prop("checked", false);

                    }
                }
                if (data.state == "success") {
                    // top.wrapper.tabCurrentClose();
                    $("#tbList").datagrid("reload");

                }
            }
        });
    }
    else if (BomEdit.settings.Action == "editMs") {
        com.ajax({
            url: "/Logic/TM/OrgDataAjax.aspx?method=EditOrgDataMs", data: { data: JSON.stringify(array), TaskId: BomEdit.settings.TaskId, CopySwitch: BomEdit.settings.CopySwitch }, success: function (data) {
                com.message(data.state, data.msg);
                if (data.state == "error") {

                    if (utils.ParseInt(data.returnValue) != 0) {

                        BomEdit.settings.currentTr = $("#divTableBody").find("tr:eq(" + (utils.ParseInt(data.returnValue) - 1) + ")");
                        BomEdit.settings.currentTr.css('background-color', '#0092DC').siblings().css('background-color', 'white');
                        BomEdit.settings.currentTr.find("input[name=cbx]").prop("checked", true);
                        BomEdit.settings.currentTr.siblings().find("input[name=cbx]").prop("checked", false);

                    }
                }
                if (data.state == "success") {
                    //  top.wrapper.tabCurrentClose();
                    $("#tbList").datagrid("reload");

                }
            }
        });
    }
}


BomEdit.GetXiShu = function (rowData) {

    if (rowData.IsFixedSize == "Y") {
        var banXiShu = 1;
        var xingXiShu = 1;
    }
    else {
        var banXiShu = parseFloat($("#txtBan").val());
        var xingXiShu = parseFloat($("#txtXing").val());
    }
    if (rowData.MaterialName.indexOf("钢格板") > -1 || rowData.MaterialName.indexOf("堆焊板") > -1 || rowData.MaterialName.indexOf("复合板") > -1 || rowData.MaterialName.indexOf("复合钢板") > -1 || rowData.MaterialName.indexOf("栅格板") > -1 || rowData.MaterialName.indexOf("钢板网") > -1 || rowData.MaterialName.indexOf("电焊网") > -1) {
        rowData = 1;
    } else if (rowData.MaterialName.indexOf("花纹板") > -1) {
        rowData.XiShu = banXiShu;
    }
    else {

        if (rowData.MaState == "板") {
            rowData.XiShu = banXiShu;
        }
        else if (rowData.MaState == "型") {
            rowData.XiShu = xingXiShu;
        }
        else {
            rowData.XiShu = 1;
        }
    }
}

BomEdit.SetSequence = function () {
    var $tr = $("#tbOperate").find("tr");
    for (var i = 0; i < $tr.length; i++) {
        $($tr[i]).find("#sq").val(i + 1);
    }

}

BomEdit.BatchEdit = function () {

    $("#divBatchEdit").dialog("open");
}


function divld() {
    /*两div联动的关键所在*/
    document.getElementById("divTableHead").scrollLeft = document.getElementById("divTableBody").scrollLeft;
}
$(BomEdit.init);